﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace XMM.Objects
{
    public class Watchfolder
    {
        int _WatchfolderID = 0;
        public int WatchfolderID { get { return _WatchfolderID; } }

        string _Path;
        public string Path { get { return _Path; } }

        DirectoryInfo _WatchfolderInfo;
        public DirectoryInfo WatchfolderInfo { get { return _WatchfolderInfo; } }

        List<FileInfo> _NewElements = new List<FileInfo>();
        public List<FileInfo> NewElements { get { return _NewElements; } }

        public bool IsAvailable
        {
            get
            {
                if (Directory.Exists(Path))
                    return true;

                return false;
            }
        }

        //string _NewElementsString;
        public string NewElementsString
        {
            get
            {
                string sNewElementsString = String.Empty;
                _NewElements = new List<FileInfo>();

                if (Path != String.Empty && Directory.Exists(Path))
                {
                    _WatchfolderInfo = new DirectoryInfo(Path);
                    
                    // Get Video-Elements.
                    ScanVideoFiles();

                    // Get...

                    // Generate String...
                    sNewElementsString = NewElements.Count.ToString() + " neue Elemente";
                }                

                return sNewElementsString;
            }
        }

        private void ScanVideoFiles()
        {
            List<Objects.SupportedExtension> lSupportedExtensions = Objects.SupportedExtension.getAllExtensions();

            foreach (Objects.SupportedExtension oExtension in lSupportedExtensions)
            {
                if (oExtension.MediaType == 1)
                {
                    FileInfo[] aFiles = _WatchfolderInfo.GetFiles("*." + oExtension.Extension, SearchOption.AllDirectories);
                    if (aFiles != null && aFiles.Count() > 0)
                    {
                        foreach (FileInfo oFileInfo in aFiles)
                        {
                            string sPath = oFileInfo.Directory + "\\" + oFileInfo.Name;

                            if (ItemChecks.checkFileExists(sPath) == false)
                            {
                                _NewElements.Add(oFileInfo);
                            }
                        }
                    }
                }
            }
        }

        public Watchfolder()
        {

        }

        public Watchfolder(int iWatchfolderID)
        {
            _WatchfolderID = iWatchfolderID;
            Load(WatchfolderID);
        }

        public Watchfolder(string sPath)
        {
            _Path = sPath;
        }

        public static List<Watchfolder> getAllWatchfolders()
        {
            List<Watchfolder> lAvailableWatchfolders = new List<Watchfolder>();

            try
            {
                XMMDBDataContext dbConnection = new XMMDBDataContext();

                var vWatchfolders = from watchfolders in dbConnection.t_watchfolders select watchfolders.ID;

                if (vWatchfolders != null && vWatchfolders.Count() > 0)
                {
                    foreach (int iWatchfolderID in vWatchfolders)
                    {
                        Watchfolder oWatchfolder = new Watchfolder(iWatchfolderID);
                        lAvailableWatchfolders.Add(oWatchfolder);
                    }
                }
            }
            catch (Exception ex)
            {
                System.Windows.MessageBox.Show("Fehler beim Abrufen aller Watchfolder: " + ex.Message, "Fehler", System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Error);
            }

            return lAvailableWatchfolders;
        }

        public bool Delete()
        {
            return Delete(WatchfolderID);
        }

        public static bool Delete(int iWatchfolderID)
        {
            try
            {
                XMMDBDataContext dbConnection = new XMMDBDataContext();

                t_watchfolders vWatchfolder = (from watchfolder in dbConnection.t_watchfolders where watchfolder.ID == iWatchfolderID select watchfolder).Single();

                if (vWatchfolder != null && vWatchfolder.ID > 0)
                {
                    dbConnection.t_watchfolders.DeleteOnSubmit(vWatchfolder);
                    dbConnection.SubmitChanges();
                    return true;
                }
            }
            catch (Exception ex)
            {
                System.Windows.MessageBox.Show("Fehler beim Löschen der Ordnerverknüpfung: " + ex.Message, "Fehler", System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Error);
            }

            return false;
        }

        public bool Load(int iWatchfolderID)
        {
            if (iWatchfolderID > 0)
            {
                try
                {
                    XMMDBDataContext dbConnection = new XMMDBDataContext();

                    t_watchfolders vWatchfolder = (from watchfolder in dbConnection.t_watchfolders where watchfolder.ID == iWatchfolderID select watchfolder).Single();

                    if (vWatchfolder != null && vWatchfolder.ID != 0)
                    {
                        _Path = vWatchfolder.Path;
                        _WatchfolderID = iWatchfolderID;

                        return true;
                    }
                }
                catch (Exception ex)
                {
                    System.Windows.MessageBox.Show("Fehler beim Laden des Watchfolders '" + iWatchfolderID.ToString() + "': " + ex.Message, "Fehler", System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Error);
                }
            }

            return false;
        }

        public bool SaveNew()
        {
            if (Path != String.Empty)
            {
                try
                {
                    XMMDBDataContext dbConnection = new XMMDBDataContext();

                    t_watchfolders vWatchfolder = new t_watchfolders();
                    vWatchfolder.Path = Path;

                    dbConnection.t_watchfolders.InsertOnSubmit(vWatchfolder);
                    dbConnection.SubmitChanges();

                    return true;
                }
                catch (Exception ex)
                {
                    System.Windows.MessageBox.Show("Fehler beim Speichern des Watchfolders: " + ex.Message, "Fehler", System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Error);
                }
            }

            return false;
        }

        public bool SaveChanges()
        {
            if (WatchfolderID > 0)
            {
                try
                {
                    XMMDBDataContext dbConnection = new XMMDBDataContext();

                    var vWatchfolder = (from watchfolder in dbConnection.t_watchfolders where watchfolder.ID == WatchfolderID select watchfolder).Single();

                    if (vWatchfolder != null)
                    {
                        vWatchfolder.Path = Path;
                        dbConnection.SubmitChanges();
                        return true;
                    }
                }
                catch (Exception ex)
                {
                    System.Windows.MessageBox.Show("Fehler beim Speichern des Watchfolders '" + WatchfolderID.ToString() + "': " + ex.Message, "Fehler", System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Error);
                }
            }

            return false;
        }
    }
}
